﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities.QuickSql;
using MySql.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Data;
using System.IO;

namespace Deviseur_Cs2i_Car
{
    class VoiCouleurCarross
    {
        int NumVoi;
        double NumCouleurCarross;

        string image;

        public VoiCouleurCarross() { }
        public VoiCouleurCarross(int NumVoi)
        {
            this.NumVoi = NumVoi;
        }
        public VoiCouleurCarross(double NumCouleurCarross)
        {
            this.NumCouleurCarross = NumCouleurCarross;
        }
        public VoiCouleurCarross(int NumVoi, double NumCouleurCarross)
        {
            this.NumVoi = NumVoi;
            this.NumCouleurCarross = NumCouleurCarross;
        }
        public int get_NumVoi()
        {
            return this.NumVoi;
        }
        public double get_NumCouleurCarross()
        {
            return this.NumCouleurCarross;
        }
        public void set_NumVoi(int NumVoi)
        {
            this.NumVoi = NumVoi;
        }
        public void set_NumCouleurCarross(double NumCouleurCarross)
        {
            this.NumCouleurCarross = NumCouleurCarross;
        }
        public void set_Image(string _image)
        {
            this.image = _image;
        }

        public void new_VoiCouleurCarross()
        {
            string ConnnectionStr = "Database=" + Program.Database + ";Data Source=" + Program.Address + ";Port=" + Program.Port + ";User Id=" + Program.Login + ";Password=" + Program.Password;

            FileStream fs = new FileStream(this.image, FileMode.Open, FileAccess.Read);

            int fileLength = (int)fs.Length;

            byte[] rawdata = new byte[fileLength];
                
            fs.Read(rawdata, 0, (int)fileLength);

            MySqlCommand cmd = new MySqlCommand();

            String sql = "INSERT INTO Voi_Couleur (NumVoi, NumCouleurCaross, Image) VALUES (" + this.NumVoi + ", " + this.NumCouleurCarross + ", @Image)";

            cmd.Connection = new MySqlConnection(ConnnectionStr);
            cmd.Connection.Open();

            cmd.CommandText = sql;

            cmd.Parameters.AddWithValue("@Image", rawdata);

            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            fs.Close();
        }

        public void delete_VoiCouleurCarross(int _NumVoi, double _NumCouleurCarross)
        {
            Program.Db.Execute("DELETE FROM Voi_Couleur WHERE NumVoi = " + _NumVoi + " AND NumCouleurCaross = " + _NumCouleurCarross);
        }
    }
}
